package com.xxl.shoppingapp.ui.user;


import android.content.Intent;
import android.text.Editable;
import android.view.View;
import android.widget.TextView;

import com.xxl.baselib.baseui.widget.ClearEditText;
import com.xxl.baselib.baseui.widget.RoundButton;
import com.xxl.baselib.baseui.widget.TextWatcher;
import com.xxl.baselib.util.ToastUtils;
import com.xxl.shoppingapp.R;
import com.xxl.shoppingapp.base.BaseActivity;
import com.xxl.shoppingapp.entity.BaseResponse;
import com.xxl.shoppingapp.http.ResponseListener;
import com.xxl.shoppingapp.http.ShoppingRequest;
import com.xxl.shoppingapp.ui.ShopAppUIHelper;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @author xuelian.xiong 2018/4/9
 * @Package com.xxl.shoppingapp.ui
 * @Title: LoginActivity
 * @Description: (用一句话描述该文件做什么)
 * Create DateTime: 2018/4/9
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.et_mobile)
    ClearEditText etMobile;
    @Bind(R.id.et_code)
    ClearEditText etCode;
    @Bind(R.id.tv_agreement)
    TextView tvAgreement;
    @Bind(R.id.bt_next)
    RoundButton btNext;
    @Bind(R.id.tv_no_code)
    TextView tvNoCode;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("手机快速注册");
        setHasBack(true);
        etMobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setNextEnable();
            }
        });
        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                setNextEnable();
            }
        });
    }

    @OnClick({R.id.et_mobile, R.id.et_code, R.id.bt_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_mobile:
                break;
            case R.id.et_code:
                break;
            case R.id.bt_next:
                ShoppingRequest.register(etMobile.getText().toString(), etCode.getText().toString(), "", "", new ResponseListener() {
                    @Override
                    public void onSuccess(String response) {
                        ShopAppUIHelper.navLogin(RegisterActivity.this);
                    }

                    @Override
                    public void onFailed(String error) {
                        ToastUtils.show(error);
                    }

                    @Override
                    public void onFinish() {

                    }
                });
                break;
        }
    }

    private void setNextEnable() {
        if (etMobile.getText().toString().length() < 11 || etCode.getText().toString().length() == 0) {
            btNext.setEnabled(false);
        } else {
            btNext.setEnabled(true);
        }
    }
}